package com.nineclock.system.controller;

import com.nineclock.common.entity.PageResult;
import com.nineclock.common.entity.Result;
import com.nineclock.system.dto.DepartmentOrUserSimpleDTO;
import com.nineclock.system.dto.SysCompanyUserDTO;
import com.nineclock.system.dto.SysDepartmentDTO;
import com.nineclock.system.service.SysCompanyUserService;
import com.nineclock.system.service.SysDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@Api(value = "组织管理接口", tags = "系统服务")
@RequestMapping("organization")
public class SysOrganizationController {
    @Autowired
    private SysCompanyUserService sysCompanyUserService;
    @Autowired
    private SysDepartmentService sysDepartmentService;

    /**
     * 组织架构: 查询组织所有员工
     * 接口路径：GET/sys/organization/members/simple
     */
    @GetMapping("members/simple")
    public Result<List<SysCompanyUserDTO>> findAllCompanyUsers() {
        List<SysCompanyUserDTO> sysCompanyUserDTOList = sysCompanyUserService.findAllCompanyUsers();
        return Result.success(sysCompanyUserDTOList);
    }
    /**
     * 组织架构: PC-获取组织架构列表(左侧菜单)
     * 接口路径：GET/sys/organization/department
     */
    @ApiOperation("PC-获取组织架构列表(左侧菜单)")
    @GetMapping("department")
    public Result<List<SysDepartmentDTO>> getDepartment() {
        List<SysDepartmentDTO> sysDepartmentDTOList = sysDepartmentService.getDepartment();
        return Result.success(sysDepartmentDTOList);
    }
    /**
     * 组织架构:PC-获取部门成员列表
     * 接口路径：GET/sys/organization/members
     */
    @ApiOperation("组织架构:PC-获取部门成员列表")
    @GetMapping("members")
    public Result<PageResult<SysCompanyUserDTO>> getDepartmentMembers(@RequestParam(defaultValue = "1") Integer page,
                                                                      @RequestParam(defaultValue = "10") Integer pageSize,
                                                                      @RequestParam(required = false) Long departmentId,
                                                                      @RequestParam(required = false) String keyword){
        PageResult<SysCompanyUserDTO> sysCompanyUserDTOList = sysCompanyUserService.getDepartmentMembers(page,pageSize,departmentId,keyword);
        return Result.success(sysCompanyUserDTOList);
    }
    /**
     * 组织架构: PC-直接导入员工数据
     * 接口路径：POST/sys/organization/uploadExcel
     */
    @ApiOperation("PC-直接导入员工数据")
    @PostMapping("uploadExcel")
    public Result uploadExcel(MultipartFile excelFile) throws Exception{
        sysCompanyUserService.uploadExcel(excelFile);
        return Result.success();
    }
    /**
     * 查询企业所有部门id
     */
    @ApiOperation("查询企业所有部门id")
    @GetMapping("getAllDepartmentId")
    public Result<List<Long>> getAllDepartmentId(){
        List<Long> idList = sysDepartmentService.getAllDepartmentId();
        return Result.success(idList);
    }
    /**
     * 根据部门ID集合查询部门下的员工数量
     */
    @ApiOperation("根据部门ID集合查询部门下的员工数量")
    @GetMapping("getAllUserByDepartmentIdList")
    public Result<Integer> getAllUserByDepartmentIdList(@RequestParam List<Long> departmentIds){
        Integer sum = sysCompanyUserService.getAllUserByDepartmentIdList(departmentIds);
        return Result.success(sum);
    }
    /**
     * 组织架构:获取部门简单列表
     * 接口路径：GET/sys/organization/simple
     */
    @ApiOperation("组织架构:获取部门简单列表")
    @GetMapping("simple")
    public Result<List<DepartmentOrUserSimpleDTO>> querySimpleDepartment(Long id, Integer includeMember){
        List<DepartmentOrUserSimpleDTO> departmentOrUserSimpleDTOList = sysDepartmentService.querySimpleDepartment(id,includeMember);
        return Result.success(departmentOrUserSimpleDTOList);
    }
    /**
     * 根据员工ID，依次获取其 所在部门的ID - 所在部门的上级部门的ID - 所在部门的上级部门的上级部门的ID ----> 有序
     */
    @ApiOperation(value="根据员工id获得从低到高级别的部门ID集合")
    @GetMapping("/allDepId/{companyUserId}")
    public Result<List<Long>> getAllDepIdByCompanyUserId(@PathVariable Long companyUserId){
        List<Long> allDepId = sysDepartmentService.getAllDepIdByCompanyUserId(companyUserId);
        return Result.success(allDepId);
    }
}
